package com.mvc.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.common.utils.StringUtil;
import com.jfinal.plugin.activerecord.Page;
import com.mvc.model.Dead;
import com.mvc.model.User;
import com.mvc.model.Venue;
import com.mvc.request.ReqPage;
import com.mvc.request.ReqVenueDelete;
import com.mvc.request.ReqVenueDetail;
import com.mvc.request.ReqVenueUserPage;
import com.mvc.request.ReqVenueAdd;
import com.mvc.request.ReqVenueUpdate;
import com.mvc.request.ReturnData;
import com.mvc.response.RspDead;
import com.mvc.response.RspPage;
import com.mvc.response.RspVenue;
import com.mvc.response.RspVenueDetail;
import com.mvc.service.VenueService;

public class VenueServiceImpl implements VenueService {

	@Override
	public ReturnData<Boolean> create(ReqVenueAdd req) throws RuntimeException {
		if (req == null) {
			return new ReturnData<Boolean>(null, 500, "请求参数不完整!");
		}

		// 1 是否登录
		if (req.getUserId() <= 0) {
			return new ReturnData<Boolean>(false, 202, "请登录!");
		}

		// 2 用户是否存在
		User user = User.dao.findById(req.getUserId());
		if (user == null) {
			return new ReturnData<Boolean>(false, 202, "当前用户不存在,请重新登录!");
		}
		// 3 判断用户mac 与 数据库mac是否相同返回都是 错误400
		if (!req.getFinalData().getMac().equals(user.getStr("mac"))) {
			return new ReturnData<Boolean>(false, 400, "当前用户已在其他设备登录，请重新登录!");
		}

		String name = req.getName();
		if (StringUtil.isEmpty(name)) {
			return new ReturnData<Boolean>(false, 202, "请填写纪念馆名称!");
		}

		// 大厅风格ID
		int styleId = req.getStyleId();
		if (styleId <= 0) {
			return new ReturnData<Boolean>(false, 202, "请选择主页风格!");
		}
		// 祭拜大厅风格Id
		int worshipId = req.getWorshipId();
		if (worshipId <= 0) {
			return new ReturnData<Boolean>(false, 202, "请选择大厅风格!");
		}
		// 墓志铭
		String eitaph = req.getEitaph();
		if (StringUtil.isEmpty(eitaph)) {
			return new ReturnData<Boolean>(false, 202, "墓志铭不能为空!");
		}
		// 逝者集合对象，参考文档Dead类
		List<RspDead> dead = req.getDead();
		if (dead == null || dead.isEmpty()) {
			return new ReturnData<Boolean>(false, 202, "请添加成员!");
		}
		// 场馆类型：1 个人，2双人 ，3 名人
		int type = req.getType();

		// 双人馆 成员关系不能空
		// 馆成员关系
		String relationship = req.getRelationship();
		if (type == 2) {
			if (StringUtil.isEmpty(relationship)) {
				return new ReturnData<Boolean>(false, 202, "请填写馆成员关系!");
			}
			if (dead.size() < 2) {
				return new ReturnData<Boolean>(false, 202, "请添加成员!");
			}
		}

		boolean isSucce = Venue.dao.add(req);

		if (isSucce) {
			return new ReturnData<Boolean>(true, 200, "新建成功!");
		} else {
			return new ReturnData<Boolean>(true, 201, "新建失败!");
		}

	}

	@Override
	public ReturnData<Boolean> update(ReqVenueUpdate req)
			throws RuntimeException {
		if (req == null) {
			return new ReturnData<Boolean>(null, 500, "请求参数不完整!");
		}

		// 1 是否登录
		if (req.getUserId() <= 0) {
			return new ReturnData<Boolean>(false, 202, "请登录!");
		}

		// 2 用户是否存在
		User user = User.dao.findById(req.getUserId());
		if (user == null) {
			return new ReturnData<Boolean>(false, 202, "当前用户不存在,请重新登录!");
		}
		// 3 判断用户mac 与 数据库mac是否相同返回都是 错误400
		if (!req.getFinalData().getMac().equals(user.getStr("mac"))) {
			return new ReturnData<Boolean>(false, 400, "当前用户已在其他设备登录，请重新登录!");
		}

		String name = req.getName();
		if (StringUtil.isEmpty(name)) {
			return new ReturnData<Boolean>(false, 202, "请填写纪念馆名称!");
		}

		// 大厅风格ID
		int styleId = req.getStyleId();
		if (styleId <= 0) {
			return new ReturnData<Boolean>(false, 202, "请选择主页风格!");
		}
		// 祭拜大厅风格Id
		int worshipId = req.getWorshipId();
		if (worshipId <= 0) {
			return new ReturnData<Boolean>(false, 202, "请选择大厅风格!");
		}
		// 墓志铭
		String eitaph = req.getEitaph();
		if (StringUtil.isEmpty(eitaph)) {
			return new ReturnData<Boolean>(false, 202, "墓志铭不能为空!");
		}
		// 逝者集合对象，参考文档Dead类
		List<RspDead> dead = req.getDead();
		if (dead == null || dead.isEmpty()) {
			return new ReturnData<Boolean>(false, 202, "请添加成员!");
		}
		// 场馆类型：1 个人，2双人 ，3 名人
		int type = req.getType();

		// 双人馆 成员关系不能空
		// 馆成员关系
		String relationship = req.getRelationship();
		if (type == 2) {
			if (StringUtil.isEmpty(relationship)) {
				return new ReturnData<Boolean>(false, 202, "请填写馆成员关系!");
			}
			if (dead.size() < 2) {
				return new ReturnData<Boolean>(false, 202, "请添加成员!");
			}
		}

		Venue venue = Venue.dao.findById(req.getVenueId());

		if (venue == null) {
			return new ReturnData<Boolean>(false, 202, "场馆不存在!");
		}

		boolean isSucce = Venue.dao.edit(venue, req);
		if (isSucce) {
			return new ReturnData<Boolean>(true, 200, "更新成功!");
		} else {
			return new ReturnData<Boolean>(true, 201, "更新失败!");
		}
	}

	@Override
	public ReturnData<Boolean> delete(ReqVenueDelete req) {

		boolean isSucce = Venue.dao.delete(req.getVenueId());
		return new ReturnData<Boolean>(isSucce, isSucce ? 200 : 201,
				isSucce ? "删除成功" : "删除失败");
	}

	@Override
	public ReturnData<RspPage<RspVenue>> queryNewPage(ReqPage req) {
		Page<Venue> page = Venue.dao.queryNewPage(req.getNowPage(),
				req.getPageSize());

		RspPage<RspVenue> venuePage = new RspPage<>();
		venuePage.setNowPage(req.getNowPage());
		venuePage.setTotalPage(page.getTotalPage());
		venuePage.setList(getRspVenues(page.getList()));
		return new ReturnData<RspPage<RspVenue>>(venuePage, 200, "ok");
	}

	@Override
	public ReturnData<RspPage<RspVenue>> queryWorshipPage(ReqPage req) {
		Page<Venue> page = Venue.dao.queryWorshipPage(req.getNowPage(),
				req.getPageSize());
		RspPage<RspVenue> venuePage = new RspPage<>();
		venuePage.setNowPage(req.getNowPage());
		venuePage.setTotalPage(page.getTotalPage());
		venuePage.setList(getRspVenues(page.getList()));
		return new ReturnData<RspPage<RspVenue>>(venuePage, 200, "ok");
	}

	@Override
	public ReturnData<RspPage<RspVenue>> queryRankingPage(ReqPage req) {
		Page<Venue> page = Venue.dao.queryRankingPage(req.getNowPage(),
				req.getPageSize());

		RspPage<RspVenue> venuePage = new RspPage<>();
		venuePage.setNowPage(req.getNowPage());
		venuePage.setTotalPage(page.getTotalPage());
		venuePage.setList(getRspVenues(page.getList()));
		return new ReturnData<RspPage<RspVenue>>(venuePage, 200, "ok");
	}

	public ReturnData<List<RspVenue>> queryUserPage(ReqVenueUserPage req) {
		if (req == null) {
			return new ReturnData<List<RspVenue>>(null, 500, "请求参数不完整!");
		}

		if (req.getUserId() <= 0) {
			return new ReturnData<List<RspVenue>>(null, 202, "请登录!");
		}
		// 2 用户是否存在
		User user = User.dao.findById(req.getUserId());
		if (user == null) {
			return new ReturnData<List<RspVenue>>(null, 202, "当前用户不存在,请重新登录!");
		}
		// 3 判断用户mac 与 数据库mac是否相同返回都是 错误400
		if (!req.getFinalData().getMac().equals(user.getStr("mac"))) {
			return new ReturnData<List<RspVenue>>(null, 400,
					"当前用户已在其他设备登录，请重新登录!");
		}

		List<Venue> list = Venue.dao.queryUserPage(req.getUserId());

		return new ReturnData<List<RspVenue>>(getRspVenues(list), 200, "ok");

	}

	private List<RspVenue> getRspVenues(List<Venue> page) {
		List<RspVenue> venues = new ArrayList<>();

		if (page != null && !page.isEmpty()) {
			for (Venue venue : page) {

				RspVenue venueRsp = new RspVenue();
				venueRsp.setId(venue.getInt(Venue.TableField.ID));
				venueRsp.setName(venue.getStr(Venue.TableField.NAME));
				venueRsp.setRelation(venue
						.getStr(Venue.TableField.RELATIONSHIP));
				venueRsp.setEpitaph(Venue.TableField.DESCNBE);
				venueRsp.setType(venue.getInt(Venue.TableField.TYPE));
				venueRsp.setGrade(venue.getInt(Venue.TableField.GRADE));
				venueRsp.setMoney(venue.getInt(Venue.TableField.MONEY));
				venueRsp.setDescnbe(venue.getStr(Venue.TableField.DESCNBE));
				List<Dead> deads = venue.getDeadList();
				if (deads != null && !deads.isEmpty()) {
					venueRsp.setDeadAHead(deads.get(0).getStr(
							Dead.TableField.HEAD));
					if (deads.size() == 2) {
						venueRsp.setDeadBHead(deads.get(1).getStr(
								Dead.TableField.HEAD));
					}
				}

				venues.add(venueRsp);
			}
		}

		return venues;
	}

	@Override
	public ReturnData<RspVenueDetail> getVenueDetail(ReqVenueDetail req) {

		if (req == null) {
			return new ReturnData<RspVenueDetail>(null, 500, "请求参数不完整!");
		}
		if (req.getVenueId() <= 0) {
			return new ReturnData<RspVenueDetail>(null, 202, "请求参数不完整");
		}

		Venue venue = Venue.dao.findById(req.getVenueId());
		if (venue == null) {
			return new ReturnData<RspVenueDetail>(null, 202, "场馆详情获取失败");
		}
		RspVenueDetail venueDetail = new RspVenueDetail();
		venueDetail.setVenue(venue);
		venueDetail.setStyle(venue.getStyle());
		venueDetail.setWorship(venue.getWorshipStyle());
		venueDetail.setDead(venue.getDeadList());
		return new ReturnData<RspVenueDetail>(venueDetail, 200, "OK");
	}
}
